<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>各JDK新特性 | 牵风散步の雲</title><meta name="author" content="牵风散步の雲"><meta name="copyright" content="牵风散步の雲"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="ffffff"><meta name="description" content="JDK81.Lambda表达式  2.新的日期API  3.引入Optional  4.使用Base64  5.接口的默认方法和静态方法  6.新增方法引用格式  7.新增Stream类  8.注解相关的改变  9.支持并行（parallel）数组  10.对并发类（Concurrency）的扩展。  JDK9 jshell(Real-Eval-Print-Loop)  String底层变更：在">
<meta property="og:type" content="article">
<meta property="og:title" content="各JDK新特性">
<meta property="og:url" content="https://kirito2493381254.gitee.io/kirito/2023/05/06/JDK/index.html">
<meta property="og:site_name" content="牵风散步の雲">
<meta property="og:description" content="JDK81.Lambda表达式  2.新的日期API  3.引入Optional  4.使用Base64  5.接口的默认方法和静态方法  6.新增方法引用格式  7.新增Stream类  8.注解相关的改变  9.支持并行（parallel）数组  10.对并发类（Concurrency）的扩展。  JDK9 jshell(Real-Eval-Print-Loop)  String底层变更：在">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://kirito2493381254.gitee.io/kirito/img/artical5.jpg">
<meta property="article:published_time" content="2023-05-06T10:57:40.177Z">
<meta property="article:modified_time" content="2023-08-02T04:59:36.665Z">
<meta property="article:author" content="牵风散步の雲">
<meta property="article:tag" content="Java基础">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://kirito2493381254.gitee.io/kirito/img/artical5.jpg"><link rel="shortcut icon" href="/kirito/img/favicon.png"><link rel="canonical" href="https://kirito2493381254.gitee.io/kirito/2023/05/06/JDK/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/kirito/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/kirito/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '',
  dateSuffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '各JDK新特性',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-08-02 12:59:36'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', 'ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/kirito/atom.xml" title="牵风散步の雲" type="application/atom+xml">
</head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/kirito/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/kirito/archives/"><div class="headline">Articles</div><div class="length-num">21</div></a><a href="/kirito/tags/"><div class="headline">Tags</div><div class="length-num">12</div></a><a href="/kirito/categories/"><div class="headline">Categories</div><div class="length-num">8</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/kirito/"><i class="fa-fw fas fa-home"></i><span> 首頁</span></a></div><div class="menus_item"><a class="site-page" href="/kirito/archives/"><i class="fa-fw fas fa-archive"></i><span> 時間軸</span></a></div><div class="menus_item"><a class="site-page" href="/kirito/tags/"><i class="fa-fw fas fa-tags"></i><span> 標籤</span></a></div><div class="menus_item"><a class="site-page" href="/kirito/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分類</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清單</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/kirito/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/kirito/link/"><i class="fa-fw fas fa-link"></i><span> 友鏈</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/kirito/img/artical5.jpg')"><nav id="nav"><span id="blog-info"><a href="/kirito/" title="牵风散步の雲"><span class="site-name">牵风散步の雲</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/kirito/"><i class="fa-fw fas fa-home"></i><span> 首頁</span></a></div><div class="menus_item"><a class="site-page" href="/kirito/archives/"><i class="fa-fw fas fa-archive"></i><span> 時間軸</span></a></div><div class="menus_item"><a class="site-page" href="/kirito/tags/"><i class="fa-fw fas fa-tags"></i><span> 標籤</span></a></div><div class="menus_item"><a class="site-page" href="/kirito/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分類</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清單</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/kirito/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/kirito/link/"><i class="fa-fw fas fa-link"></i><span> 友鏈</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">各JDK新特性</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2023-05-06T10:57:40.177Z" title="Created 2023-05-06 18:57:40">2023-05-06</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2023-08-02T04:59:36.665Z" title="Updated 2023-08-02 12:59:36">2023-08-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/kirito/categories/Java/">Java</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="各JDK新特性"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="JDK8"><a href="#JDK8" class="headerlink" title="JDK8"></a>JDK8</h2><pre><code>1.Lambda表达式

2.新的日期API

3.引入Optional

4.使用Base64

5.接口的默认方法和静态方法

6.新增方法引用格式

7.新增Stream类

8.注解相关的改变

9.支持并行（parallel）数组

10.对并发类（Concurrency）的扩展。
</code></pre>
<h2 id="JDK9"><a href="#JDK9" class="headerlink" title="JDK9"></a>JDK9</h2><ul>
<li><p><strong>jshell(Real-Eval-Print-Loop)</strong></p>
</li>
<li><p><strong>String底层变更：</strong>在 JDK 8 及之前的版本当中，String 的底层使用 <code>char[]</code> 数组存储内容；而在 JDK 9 之后，字符串的内容实际改用 <code>bytes[]</code> 字节形式存储，并配合 <code>encodingFlag</code> 配合编码解码。这个修改对上层代码透明，即用户层面的各种 <code>String</code> 操作没有发生任何变化。</p>
</li>
<li><p><strong>API：of创建不可变序列</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">List&lt;Integer&gt; list = List.of(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>);</span><br><span class="line">Set&lt;Integer&gt; set = Set.of(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// k1,v1,k2,v2,...,形式创建</span></span><br><span class="line">Map&lt;Integer, String&gt; map1 = Map.of(<span class="number">1</span>, <span class="string">&quot;v1&quot;</span>, <span class="number">2</span>, <span class="string">&quot;v2&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 通过 Map.entry 形式创建</span></span><br><span class="line">Map&lt;Integer, String&gt; map2 = Map.ofEntries(</span><br><span class="line">    Map.entry(<span class="number">1</span>, <span class="string">&quot;v1&quot;</span>),</span><br><span class="line">    Map.entry(<span class="number">2</span>, <span class="string">&quot;v2&quot;</span>),</span><br><span class="line">    Map.entry(<span class="number">3</span>, <span class="string">&quot;v3&quot;</span>),</span><br><span class="line">    Map.entry(<span class="number">4</span>, <span class="string">&quot;v4&quot;</span>)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>API:HTTP2协议接口</strong>：JDK 9 中引入了新的 API 以支持 HTTP 2.0 版本</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// throws IOException, InterruptedException</span></span><br><span class="line"><span class="comment">// 构建 client, 支持 http 2.0</span></span><br><span class="line"><span class="type">HttpClient</span> <span class="variable">client</span> <span class="operator">=</span> HttpClient.newBuilder().version(HttpClient.Version.HTTP_2).build();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 构建请求</span></span><br><span class="line"><span class="type">HttpRequest</span> <span class="variable">req</span> <span class="operator">=</span> HttpRequest.newBuilder(URI.create(<span class="string">&quot;https://www.baidu.com&quot;</span>)).build();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 发送请求</span></span><br><span class="line">HttpResponse&lt;String&gt; resp = client.send(req,HttpResponse.BodyHandlers.ofString());</span><br><span class="line">System.out.println(resp.body());</span><br></pre></td></tr></table></figure>

<h3 id="JDK9模块化"><a href="#JDK9模块化" class="headerlink" title="JDK9模块化"></a>JDK9模块化</h3><p>JDK 9 引入了模块化特性，称 Java Platform Module System，简称 JPMS，或者 Project jigsaw。模块化的优势有三点：</p>
<ol>
<li>改进组件之间的依赖管理，引入比 jar 包粒度更大的 Module。</li>
<li>使得 java 程序更容易轻量级部署。</li>
<li>改进性能和安全性。</li>
</ol>
<p>模块化还使得精简庞大的 JRE 成为可能。</p>
<h2 id="JDK10"><a href="#JDK10" class="headerlink" title="JDK10"></a>JDK10</h2><ul>
<li><strong>引入关键字var</strong></li>
</ul>
<p>JDK 10 支持使用 <code>var</code> 简化局部变化的类型声明，比如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 显然 arrayList 是一个 ArrayList&lt;Integer&gt; 类型。</span></span><br><span class="line">ArrayList&lt;Integer&gt; arrayList = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br></pre></td></tr></table></figure>

<p>适当的精简可以提高代码的可读性，其 <code>arrayList</code> 的实际类型将交给编译器进行推断。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">var</span> <span class="variable">arrayList</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;Integer&gt;();</span><br></pre></td></tr></table></figure>



<h2 id="JDK11"><a href="#JDK11" class="headerlink" title="JDK11"></a>JDK11</h2><ul>
<li><strong>API: 字符串增强：</strong>JDK 11 新增了部分对字符串的处理方法。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">println(<span class="string">&quot;&quot;</span>.isBlank());      <span class="comment">// 判断字符串是否为 &quot;&quot;.</span></span><br><span class="line">println(<span class="string">&quot;  welcome to java 17 &quot;</span>.strip());  <span class="comment">// 去掉首尾空格</span></span><br><span class="line">println(<span class="string">&quot;  welcome to java 17&quot;</span>.stripLeading()); <span class="comment">// 去掉首部空格</span></span><br><span class="line">println(<span class="string">&quot;welcome to java 17 &quot;</span>.stripTrailing()); <span class="comment">// 去掉尾部空格</span></span><br><span class="line">println(<span class="string">&quot;word&quot;</span>.repeat(<span class="number">2</span>));     <span class="comment">// &quot;wordword&quot;</span></span><br></pre></td></tr></table></figure>

<ul>
<li><strong>Lambda表达式推导类型</strong>：JDK 11 允许将 <code>var</code> 关键字应用到 lambda 表达式中，起到简化声明的作用。比如：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@FunctionalInterface</span></span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">Mapper</span>&lt;A,B&gt;&#123;</span><br><span class="line">       B <span class="title function_">map</span><span class="params">(A a)</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// var 关键字现可以用于 lambda 表达式。</span></span><br><span class="line">Mapper&lt;String,Integer&gt; string2int = (<span class="keyword">var</span> a) -&gt; a.length(); </span><br></pre></td></tr></table></figure>



<h2 id="JDK13"><a href="#JDK13" class="headerlink" title="JDK13"></a>JDK13</h2><ul>
<li><strong>增强switch</strong>：增强版 switch 在 JDK 12 作为预览特性引入。在 JDK 14 之后，增强版 <code>switch</code> 语句块具备返回值。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">var</span> <span class="variable">simple</span> <span class="operator">=</span> <span class="keyword">switch</span> (lang) &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&quot;java&quot;</span> -&gt; <span class="string">&quot;j&quot;</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&quot;go&quot;</span>   -&gt; <span class="string">&quot;g&quot;</span>;</span><br><span class="line">    <span class="keyword">default</span> -&gt; <span class="string">&quot;&lt;non&gt;&quot;</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>在新版本的 <code>switch</code> 语句中，分支创建可以使用 <code>-&gt;</code> 符号，且 <code>case</code>s 的判断不会引发穿透现象，因此不需要显式地在每一个分支后面标注 <code>break</code> 了。下面例子展示了增强 <code>switch</code> 的更多特性：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">var</span> <span class="variable">simple</span> <span class="operator">=</span> <span class="keyword">switch</span> (lang) &#123;</span><br><span class="line">    <span class="comment">// 1. 允许在一个 case 中匹配多个值    </span></span><br><span class="line">    <span class="keyword">case</span> <span class="string">&quot;java&quot;</span>,<span class="string">&quot;scala&quot;</span>,<span class="string">&quot;groovy&quot;</span> -&gt; <span class="string">&quot;jvm&quot;</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&quot;c&quot;</span>,<span class="string">&quot;cpp&quot;</span> -&gt; <span class="string">&quot;c&quot;</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&quot;go&quot;</span>   -&gt; <span class="string">&quot;g&quot;</span>;</span><br><span class="line">    <span class="keyword">default</span> -&gt; &#123;</span><br><span class="line">        <span class="comment">// 2. 复杂分支内返回值使用 yield 关键字 (相当于 return)</span></span><br><span class="line">        <span class="keyword">if</span>(lang == <span class="literal">null</span>)&#123;</span><br><span class="line">            <span class="keyword">yield</span> <span class="string">&quot;&lt;unknown&gt;&quot;</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">yield</span> <span class="string">&quot;&lt;non&gt;&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<ul>
<li><strong>文本块TextBlock</strong>：JDK 13 允许使用三引号 <code>&quot;&quot;&quot;</code> 表示长文本内容。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">var</span> <span class="variable">block</span> <span class="operator">=</span> <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        lang: java</span></span><br><span class="line"><span class="string">        version: 13</span></span><br><span class="line"><span class="string">        dbname: mysql</span></span><br><span class="line"><span class="string">        ip: 192.168.140.2</span></span><br><span class="line"><span class="string">        usr: root</span></span><br><span class="line"><span class="string">        pwd: 1000</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 6 行</span></span><br><span class="line">block.lines().count();</span><br></pre></td></tr></table></figure>

<p>文本块内允许插入 <code>\</code> 阻止换行，如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">var</span> <span class="variable">block</span> <span class="operator">=</span> <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        lang: java\</span></span><br><span class="line"><span class="string">        version: 13\</span></span><br><span class="line"><span class="string">        dbname: mysql\</span></span><br><span class="line"><span class="string">        ip: 192.168.140.2\</span></span><br><span class="line"><span class="string">        usr: root\</span></span><br><span class="line"><span class="string">        pwd: 1000</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span>;</span><br><span class="line"><span class="comment">// 实际效果：            </span></span><br><span class="line"><span class="comment">// &quot;lang:javaversion: 13dbname: mysql...&quot;</span></span><br><span class="line"><span class="comment">// 1 行</span></span><br><span class="line">block.lines().count();</span><br></pre></td></tr></table></figure>

<p>另外注意，每行末尾的空格会被忽略，除非主动将其替换为 <code>/s</code>。</p>
<h2 id="JDK16"><a href="#JDK16" class="headerlink" title="JDK16"></a>JDK16</h2><ul>
<li><strong>instanceof模式匹配</strong>：如果一个上转型对象的具体类型已经被 <code>instanceof</code> 关键字确定，那么其后续操作可省略强制转换，见下面的示例：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// o 可能是 String 类型或者是 Double 类型。</span></span><br><span class="line"><span class="type">Object</span> <span class="variable">o</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Random</span>().nextInt() % <span class="number">2</span> == <span class="number">0</span> ?  <span class="string">&quot;java16&quot;</span> : <span class="number">1000.0d</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// s 相当于被确定类型之后的 o。</span></span><br><span class="line"><span class="keyword">if</span>(o <span class="keyword">instanceof</span> String s)&#123;</span><br><span class="line">    <span class="comment">// 不再需要 ((String)o).length()</span></span><br><span class="line">    System.out.println(s.length());</span><br><span class="line">&#125;<span class="keyword">else</span> &#123;</span><br><span class="line">    System.out.println();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><strong>Records</strong>：被 <code>record</code> 关键字修饰的类相当于 Scala 的样例类 <code>case class</code>，或者可看成是 Lombok 中 <code>@Data</code> 注解的一个 “低配” 替代。编译器会一次性代替用户生成 <code>getters</code>，<code>constructors</code>，<code>toString</code> 等方法。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">record</span> <span class="title class_">Student</span><span class="params">(String name,Integer age)</span>&#123;&#125;</span><br><span class="line"><span class="comment">// ....</span></span><br><span class="line"><span class="type">var</span> <span class="variable">student</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;Wang Fang&quot;</span>,<span class="number">13</span>);</span><br><span class="line">student.age();</span><br><span class="line">student.name();</span><br></pre></td></tr></table></figure>



<h2 id="JDK17"><a href="#JDK17" class="headerlink" title="JDK17"></a>JDK17</h2><ul>
<li><strong>密封类</strong>：其 Sealed class 的概念和 Scala 类似，<strong>密封类不允许在包外被继承拓展</strong>，密封类必须具备子类。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 密封类</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">sealed</span> <span class="keyword">class</span> <span class="title class_">People</span> &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 密封类 People 必须至少有一个子类。</span></span><br><span class="line"><span class="comment">// 非密封的 People 子类</span></span><br><span class="line"><span class="keyword">non-sealed</span> <span class="keyword">class</span> <span class="title class_">Teacher</span> <span class="keyword">extends</span> <span class="title class_">People</span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 密封的 People 子类。</span></span><br><span class="line"><span class="keyword">sealed</span> <span class="keyword">class</span> <span class="title class_">Driver</span> <span class="keyword">extends</span> <span class="title class_">People</span>&#123;&#125;</span><br><span class="line"><span class="keyword">non-sealed</span> <span class="keyword">class</span> <span class="title class_">TruckDriver</span> <span class="keyword">extends</span> <span class="title class_">Driver</span>&#123;&#125;</span><br></pre></td></tr></table></figure>

<p>密封类可以声明子类，但必须需要严格声明其子类是密封 ( <code>sealed</code> ) 或者是非密封 ( <code>non-sealed</code> ) 的。以下是 JDK 17 的<strong>预览功能</strong>：<code>permits</code> 关键字可进一步限制同一个包下有哪些类允许继承它。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 即使在一个包下，也只有 Teacher 和 Driver 被允许继承 People。</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">sealed</span> <span class="keyword">class</span> <span class="title class_">People</span> <span class="keyword">permits</span> Teacher,Driver&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">non-sealed</span> <span class="keyword">class</span> <span class="title class_">Teacher</span> <span class="keyword">extends</span> <span class="title class_">People</span>&#123;&#125;</span><br><span class="line"><span class="keyword">non-sealed</span> <span class="keyword">class</span> <span class="title class_">Driver</span> <span class="keyword">extends</span> <span class="title class_">People</span>&#123;&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><strong>增强 switch（jdk17）</strong>：JDK 17 对 switch 语句做了进一步增强，它现在支持匹配类型以及判空的功能：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">Number</span> <span class="variable">v1</span> <span class="operator">=</span> <span class="number">3.1415d</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">switch</span> (v1) &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="literal">null</span> -&gt; println(<span class="string">&quot;null&quot;</span>);</span><br><span class="line">    <span class="keyword">case</span> Float f -&gt; println(f);</span><br><span class="line">    <span class="keyword">case</span> Double d -&gt; println(d);</span><br><span class="line">    <span class="keyword">default</span> -&gt; println(<span class="string">&quot;NaN&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注，该语法目前是 JDK 17 的<strong>预览功能</strong>。</p>
<h2 id="JDK20"><a href="#JDK20" class="headerlink" title="JDK20"></a>JDK20</h2><p>JEP 429：作用域值（第一次孵化）</p>
<p>JEP 432：记录模式（第二次预览）</p>
<p>JEP 433：switch 模式匹配（第四次预览）</p>
<p>JEP 434: 外部函数和内存 API（第二次预览）</p>
<p>JEP 436: 虚拟线程（第二次预览）</p>
<p>JEP 437: 结构化并发(第二次孵化)</p>
<p>JEP 432：向量 API（第五次孵化）</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="https://kirito2493381254.gitee.io/kirito">牵风散步の雲</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="https://kirito2493381254.gitee.io/kirito/2023/05/06/JDK/">https://kirito2493381254.gitee.io/kirito/2023/05/06/JDK/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/kirito/tags/Java%E5%9F%BA%E7%A1%80/">Java基础</a></div><div class="post_share"><div class="social-share" data-image="/kirito/img/artical5.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/kirito/2023/06/09/JMM/" title="JMM"><img class="cover" src="/kirito/img/artical5.jpg" onerror="onerror=null;src='/kirito/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">JMM</div></div></a></div><div class="next-post pull-right"><a href="/kirito/2023/05/05/%E5%B8%B8%E7%94%A8%E7%9A%84Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F/" title="常用的Lambda表达式"><img class="cover" src="/kirito/img/artical.jpg" onerror="onerror=null;src='/kirito/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">常用的Lambda表达式</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>Related Articles</span></div><div class="relatedPosts-list"><div><a href="/kirito/2023/06/09/JMM/" title="JMM"><img class="cover" src="/kirito/img/artical5.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-06-09</div><div class="title">JMM</div></div></a></div><div><a href="/kirito/2023/04/17/Java%E5%8F%8D%E5%B0%84/" title="Java反射"><img class="cover" src="/kirito/img/artical2.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-04-17</div><div class="title">Java反射</div></div></a></div><div><a href="/kirito/2023/04/18/Java%E5%BA%8F%E5%88%97%E5%8C%96/" title="Java序列化"><img class="cover" src="/kirito/img/artical4.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-04-18</div><div class="title">Java序列化</div></div></a></div><div><a href="/kirito/2023/05/05/%E5%B8%B8%E7%94%A8%E7%9A%84Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F/" title="常用的Lambda表达式"><img class="cover" src="/kirito/img/artical.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-05</div><div class="title">常用的Lambda表达式</div></div></a></div></div></div><hr class="custom-hr"/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> Comment</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/kirito/img/avatar.jpg" onerror="this.onerror=null;this.src='/kirito/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">牵风散步の雲</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/kirito/archives/"><div class="headline">Articles</div><div class="length-num">21</div></a><a href="/kirito/tags/"><div class="headline">Tags</div><div class="length-num">12</div></a><a href="/kirito/categories/"><div class="headline">Categories</div><div class="length-num">8</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/Kiritoabc"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/Kiritoabc" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:2493381254@qq.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#JDK8"><span class="toc-number">1.</span> <span class="toc-text">JDK8</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JDK9"><span class="toc-number">2.</span> <span class="toc-text">JDK9</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#JDK9%E6%A8%A1%E5%9D%97%E5%8C%96"><span class="toc-number">2.1.</span> <span class="toc-text">JDK9模块化</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JDK10"><span class="toc-number">3.</span> <span class="toc-text">JDK10</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JDK11"><span class="toc-number">4.</span> <span class="toc-text">JDK11</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JDK13"><span class="toc-number">5.</span> <span class="toc-text">JDK13</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JDK16"><span class="toc-number">6.</span> <span class="toc-text">JDK16</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JDK17"><span class="toc-number">7.</span> <span class="toc-text">JDK17</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JDK20"><span class="toc-number">8.</span> <span class="toc-text">JDK20</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/kirito/2023/08/09/MySQL%E4%BA%8B%E5%8A%A1%E7%AF%87/" title="MySQL事务"><img src="/kirito/img/artical3.jpg" onerror="this.onerror=null;this.src='/kirito/img/404.jpg'" alt="MySQL事务"/></a><div class="content"><a class="title" href="/kirito/2023/08/09/MySQL%E4%BA%8B%E5%8A%A1%E7%AF%87/" title="MySQL事务">MySQL事务</a><time datetime="2023-08-09T10:21:17.207Z" title="Created 2023-08-09 18:21:17">2023-08-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/kirito/2023/08/09/MySQL%E7%B4%A2%E5%BC%95%E7%AF%87/" title="MySQL索引"><img src="/kirito/img/artical5.jpg" onerror="this.onerror=null;this.src='/kirito/img/404.jpg'" alt="MySQL索引"/></a><div class="content"><a class="title" href="/kirito/2023/08/09/MySQL%E7%B4%A2%E5%BC%95%E7%AF%87/" title="MySQL索引">MySQL索引</a><time datetime="2023-08-09T08:20:58.443Z" title="Created 2023-08-09 16:20:58">2023-08-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/kirito/2023/08/04/go-zero/" title="go-zero"><img src="/kirito/img/artical.jpg" onerror="this.onerror=null;this.src='/kirito/img/404.jpg'" alt="go-zero"/></a><div class="content"><a class="title" href="/kirito/2023/08/04/go-zero/" title="go-zero">go-zero</a><time datetime="2023-08-04T08:43:36.925Z" title="Created 2023-08-04 16:43:36">2023-08-04</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/kirito/2023/06/26/Canal/" title="Canal"><img src="/kirito/img/artical4.jpg" onerror="this.onerror=null;this.src='/kirito/img/404.jpg'" alt="Canal"/></a><div class="content"><a class="title" href="/kirito/2023/06/26/Canal/" title="Canal">Canal</a><time datetime="2023-06-26T05:53:08.133Z" title="Created 2023-06-26 13:53:08">2023-06-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/kirito/2023/06/19/Lua/" title="lua"><img src="/kirito/img/artical2.jpg" onerror="this.onerror=null;this.src='/kirito/img/404.jpg'" alt="lua"/></a><div class="content"><a class="title" href="/kirito/2023/06/19/Lua/" title="lua">lua</a><time datetime="2023-06-19T04:42:42.400Z" title="Created 2023-06-19 12:42:42">2023-06-19</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/kirito/img/artical5.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2023 By 牵风散步の雲</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="chat-btn" type="button" title="Chat"><i class="fas fa-sms"></i></button><a id="to_comment" href="#post-comment" title="Scroll To Comments"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="Back To Top"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/kirito/js/utils.js"></script><script src="/kirito/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: '',
      appKey: '',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !true) {
  if (true) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>